import axios from "axios";
import store from "./store/index";

const http = axios.create({
  // baseURL: process.env.NODE_ENV == "production" ? process.env.VUE_APP_API_URL : "/mall",
  baseURL: process.env.VUE_APP_API_URL,
});

http.defaults.timeout = 10000;
axios.interceptors.request.use = http.interceptors.request.use;
http.interceptors.request.use(
  (config) => {
    if (store.state.token) {
      config.headers["token-mall"] = store.state.token;
    }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);
// axios拦截响应
http.interceptors.response.use(
  (response) => {
    const { data: res } = response;
    if (res.code != "0") store.commit("changeCode", res);
    return response;
  },
  (err) => {
    store.commit("changeCode", err);
    console.log(err, "来自http响应拦截器");
    return Promise.reject(err);
  }
);

export default http;
